<?php
class Common {
     /**
     * 生成sql 转义特殊字符
     * @param type $string
     * @return type 
     */
     public static function strEscape($string, $flag=TRUE) {
          if($flag)
          {
            $string = str_replace('"', " ", $string);
          }
          $db=  MysqlDB::load("");
          $con=$db->get_con();
          if(function_exists("mysqli_escape_string")){
             $string =  mysqli_real_escape_string($con,$string);
          }else{
              $string =  mysql_escape_string($string);
          }
        
        return $string;
     }
     
     /**
     * 获取当前页面完整URL地址
     */
    static function getUrl() {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? self::safeReplace($_SERVER['PHP_SELF']) : self::safeReplace($_SERVER['SCRIPT_NAME']);
        $path_info = isset($_SERVER['PATH_INFO']) ? self::safeReplace($_SERVER['PATH_INFO']) : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? self::safeReplace($_SERVER['REQUEST_URI']) : $php_self . (isset($_SERVER['QUERY_STRING']) ? '?' . self::safeReplace($_SERVER['QUERY_STRING']) : $path_info);
        if($_SERVER["REQUEST_METHOD"]=='POST'){
            $querystring = http_build_query($_REQUEST);
            $relate_url="/?".$querystring; 
        }
        return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relate_url;
    }
     /**
     * 读取当前url移除分页参数
     */
    static function getCurrentUrl() {       
          $url = self::getUrl();   
     
          $querystring = substr(strstr($url, '?'), 1);
         
          parse_str($querystring, $pars);
          $query_array = array();
          foreach ($pars as $k => $v) {
              if ($k == 'prev' || $k == 'page' || $k == 'next' || $k == 'pagenum') {
                  continue;
              }
              $query_array[$k] = $v;
          }
 
          $querystring = http_build_query($query_array);
          $pos = strpos($url, '?');
          $url = substr($url, 0, $pos) . '?' . $querystring;
        
        return $url;
    }
    /**
     * 安全过滤函数
     * @param $string
     * @return string
     */
    static function safeReplace($string) {
        $string = str_replace('%20', '', $string);
        $string = str_replace('%27', '', $string);
        $string = str_replace('%2527', '', $string);
        $string = str_replace('*', '', $string);
        $string = str_replace('"', '&quot;', $string);
        $string = str_replace("'", '', $string);
        $string = str_replace('"', '', $string);
        $string = str_replace(';', '', $string);
        $string = str_replace('<', '&lt;', $string);
        $string = str_replace('>', '&gt;', $string);
        $string = str_replace("{", '', $string);
        $string = str_replace('}', '', $string);
        return $string;
    }
    public static function getPages($count, $current_page = 1, $page_size = 20)
    {
        
        $total_pages = ceil($count/$page_size);       
    
        return array(
          'url'        => self::getCurrentUrl(),
          'cur'        => $current_page,
          'count'      => $count,
          'total_pages'=> $total_pages,
          'next'       => $current_page < $total_pages ? $current_page+1 : $current_page,
          'prev'       => $current_page > 1 ? $current_page-1 : 1,
        );
        
        
    }
    
    public static function getThumb($picpath,$style){
        $upload=  UploadFile::getInstance();
         global $_G;
         $imagepath="";
         if(isset($_G["upload_image"]["image_path"])){
             $imagepath=$_G["upload_image"]["image_path"];
         }
         
         if(stripos($picpath, $imagepath)!==FALSE){
             $picpath=str_replace($imagepath, "", $picpath);
         }
      
         return $upload->getImageUrl($picpath,$style);
     }
     
     
}
